﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.Commands.EnterpriseCommands
{
    public class EnterpriseCreateHandler : IRequestHandler<EnterpriseCreateCommand, Result>
    {
        private readonly IBaseRepository<Enterprise> enterpriseRepository;

        public EnterpriseCreateHandler(IBaseRepository<Enterprise> enterpriseRepository)
        {
            this.enterpriseRepository = enterpriseRepository;
        }

        public async Task<Result> Handle(EnterpriseCreateCommand request, CancellationToken cancellationToken)
        {
            var query = new Result() { Code = 0 };
            #region 验证
            var r2 = enterpriseRepository.GetAll().Where(x => x.EnterpriseFullName == request.GetEnterprise.EnterpriseFullName).Count();
            if (r2 > 0)
            {
                query.Code = -1;
                query.Messege = "企业名称已存在";
                return query;
            }

            var r3 = enterpriseRepository.GetAll().Where(x => x.EnterpriseAbbreviation == request.GetEnterprise.EnterpriseAbbreviation).Count();
            if (r3 > 0)
            {
                query.Code = -1;
                query.Messege = "企业简称已存在";
                return query;
            }

            var r4 = enterpriseRepository.GetAll().Where(x => x.EnterpriseAddress == request.GetEnterprise.EnterpriseAddress).Count();
            if (r4 > 0)
            {
                query.Code = -1;
                query.Messege = "详细地址已存在";
                return query;
            }

            var r5 = enterpriseRepository.GetAll().Where(x => x.EnterprisePhone == request.GetEnterprise.EnterprisePhone).Count();
            if (r5 > 0)
            {
                query.Code = -1;
                query.Messege = "企业电话已存在";
                return query;
            }

            var r6 = enterpriseRepository.GetAll().Where(x => x.EnterpriseUnified == request.GetEnterprise.EnterpriseUnified).Count();
            if (r6 > 0)
            {
                query.Code = -1;
                query.Messege = "统一社会信用代码已存在";
                return query;
            }

            var r7 = enterpriseRepository.GetAll().Where(x => x.EnterpriseDescription == request.GetEnterprise.EnterpriseDescription).Count();
            if (r7 > 0)
            {
                query.Code = -1;
                query.Messege = "描述已存在";
                return query;
            }
            #endregion

            var list = new Enterprise()
            {
                EnterpriseID = request.GetEnterprise.EnterpriseID,
                EnterpriseFullName = request.GetEnterprise.EnterpriseFullName,
                EnterpriseAbbreviation = request.GetEnterprise.EnterpriseAbbreviation,
                EnterpriseUpload = request.GetEnterprise.EnterpriseUpload,
                IndustryLabelsId = request.GetEnterprise.IndustryLabelsId,
                ProvinceId = request.GetEnterprise.ProvinceId,
                CityId = request.GetEnterprise.CityId,
                CountyId = request.GetEnterprise.CountyId,
                EnterpriseAddress = request.GetEnterprise.EnterpriseAddress,
                EnterprisePhone = request.GetEnterprise.EnterprisePhone,
                EnterpriseUnified = request.GetEnterprise.EnterpriseUnified,
                EnterpriseDescription = request.GetEnterprise.EnterpriseDescription,
                EnterpriseDeactivated = false,
                EnterpriseHead = request.GetEnterprise.EnterpriseHead,
                EnterpriseFinance = request.GetEnterprise.EnterpriseFinance,
                EnterpriseCharge = request.GetEnterprise.EnterpriseCharge,
                EnterprisePerson = request.GetEnterprise.EnterprisePerson,
                AccountingUnitId = request.GetEnterprise.AccountingUnitId,
            };

            if (list != null)
            {
                var result = enterpriseRepository.Add(list);
                query.Code = 1;
                query.Messege = "添加成功";
                query.Data = list;
            }
            return query;
        }
    }
}
